import { addVehicleAPI, bindDriverAPI, deleteVehicleAPI, editVehicleAPI, endVehicleAPI, fetchDriverAllAPI, fetchDriverByIdAPI, fetchVehicleDetailAPI, fetchVehicleListAPI, getVehicleListCountAPI, startVehicleAPI, updateVehicleAPI } from '@/api/vehicle/vehicleListAPI'

const actions = {
  /*
  *获取车辆分页数据的action
  *  */
  async fetchVehicleListAction(context, pageParams) {
    const { data } = await fetchVehicleListAPI(pageParams)
    context.commit('setVehicleListMutation', data)
  },
  /*
  * 统计车辆停用和可用的action
  *  */
  async getVehicleListCountAction(context) {
    const { data } = await getVehicleListCountAPI()
    context.commit('setVehicleListCountMutation', data)
  },
  /*
  * 删除车辆的action
  *  */
  async deleteVehicleAction(context, id) {
    await deleteVehicleAPI(id)
    return '删除车辆成功'
  },
  /*
  * 新增车辆的action
  *  */
  async addVehicleAction(context, formData) {
    await addVehicleAPI(formData)
    return '新增车辆成功'
  },
  /*
  *
  * 获取所有司机的action
  *  */
  async fetchDriverAllAction(context) {
    const { data } = await fetchDriverAllAPI()
    context.commit('setDriverAllMutation', data.items)
  },

  /*
  *
  * 获取车辆所绑定的司机action
  *  */
  async fetchDriverByIdAction(context, truckId) {
    const { data } = await fetchDriverByIdAPI(truckId)
    context.commit('setDriverList', data)
  },

  /*
  *
  * 绑定司机的action
  *  */
  async bindDriverAction(context, bindInfo) {
    await bindDriverAPI(bindInfo)
    return '绑定司机成功'
  },
  /*
  * 启用车辆的action
  *  */
  async startVehicleAction(context, id) {
    await startVehicleAPI(id)
    return '车辆启用成功'
  },

  /*
  * 停用车辆的action
  *  */
  async endVehicleAction(context, id) {
    await endVehicleAPI(id)
    return '车辆停用成功'
  },

  /*
  * 获取车辆详情的action
  *  */
  async fetchVehicleDetailAction(context, id) {
    const { data } = await fetchVehicleDetailAPI(id)
    return data
  },
  /*
  *
  * 编辑车辆的action
  *  */
  async editVehicleAction(context, formData) {
    await editVehicleAPI(formData)
    return '编辑车辆成功'
  },
  /*
  *
  * 更新车辆信息的action
  *  */
  async updateVehicleAction(context, formData) {
    await updateVehicleAPI(formData)
    return '车辆信息保存成功'
  }
}
const mutations = {
  /*
  *设置车辆分页数据的mutation
  *  */
  setVehicleListMutation(state, { items, counts }) {
    state.counts = Number(counts)
    state.vehicleList = items
  },
  /*
  * 设置车辆停用和可用的mutation
  *  */
  setVehicleListCountMutation(state, count) {
    state.count = count
  },
  /*
    *
    * 设置所有司机的action
    *  */
  setDriverAllMutation(state, driverAllList) {
    state.driverAllList = driverAllList
  },
  /*
  * 设置车辆所绑定的司机mutation
  *  */
  setDriverList(state, bindDriverList) {
    state.bindDriverList = bindDriverList
  },

  /*
  *push一个绑定的司机
  *  */
  pushDriverMutation(state, id) {
    const find = state.bindDriverList.find(item => item.id === id)
    if (find) return
    const driver = state.driverAllList.find(item => item.id === id)
    state.bindDriverList.push(driver)
  },
  /*
  * 删除一个绑定的司机
  *  */
  deleteDriverMutation(state, id) {
    state.bindDriverList = state.bindDriverList.filter(item => item.id !== id)
  },
  /*
  * 清空绑定的司机
  *  */
  clearDriverMutation(state) {
    state.bindDriverList = []
  }
}
const state = () => {
  return {
    counts: 0,
    vehicleList: [],
    count: {},
    bindDriverList: [],
    driverAllList: []
  }

}

const getters = {}

export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true
}
